package controller.annotation;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
	@Autowired
	private util.ftp.IFtp ftpService;
	
	@Autowired
	private service.BillService billService;
	
	protected static Logger logger = Logger.getLogger("controller");
	
	@RequestMapping(method = RequestMethod.GET)
    public ModelAndView importFTP() {
		ModelAndView model = new ModelAndView("BillList");
		model.addObject("msg", "hello world");
 
		ArrayList<Hashtable> items = null;
		try {
			items = ftpService.getFile();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		billService.importBills(items);
		List bills = null;
		try {
			bills = billService.findAllBill();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addObject("bills", bills);
		
		return model;
	}
	
	@RequestMapping(method = RequestMethod.POST)
    public ModelAndView helloWorld() {
		ModelAndView model = new ModelAndView("HelloWorldPage");
		model.addObject("msg", "hello world");
		return model;
	}
    
    @RequestMapping(value = "/add", method = RequestMethod.GET)
	public String getAjaxAddPage() {
    	logger.debug("Received request to show AJAX, add page");
    	
    	// This will resolve to /WEB-INF/jsp/ajax-add-page.jsp
    	return "hello";
	}
}
